package com.sec.bnjey.database;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

public class DiagDbUtil {

	static final String DATABASE_TABLE = "diag_code";

	public static final String PKEY = "PKEY";

	public static final String CODE = "CODE";
	public static final String AUTO = "AUTO";
	public static final String INFO = "INFO";
	public static final String WORK_TIME = "WORK_TIME";
	public static final String HISTORY_INDEX = "HISTORY_INDEX";

	static final String CREATE_QUERY = "CREATE TABLE " + DATABASE_TABLE
			+ " (PKEY INTEGER PRIMARY KEY AUTOINCREMENT, " + CODE + " TEXT, "
			+ AUTO + " TEXT, " + INFO + " TEXT, " + WORK_TIME + " TEXT, "
			+ HISTORY_INDEX + " INTEGER)";

	private SQLiteDatabase mDatabase;

	DiagDbUtil(SQLiteDatabase db) {
		mDatabase = db;
	}

	public synchronized long insert(DiagInfo info) {
		ContentValues args = new ContentValues();
		args.put(CODE, info.getCode());
		args.put(AUTO, info.getAuto());
		args.put(INFO, info.getInfo());
		args.put(WORK_TIME, info.getWorkTime());
		args.put(HISTORY_INDEX, info.getHistoryIndex());
		return mDatabase.insert(DATABASE_TABLE, null, args);
	}

	public synchronized boolean remove(long key) {
		return mDatabase.delete(DATABASE_TABLE, PKEY + "=" + key, null) > 0;
	}

	public synchronized boolean removeByHistoryIndex(long historyIndex) {
		return mDatabase.delete(DATABASE_TABLE, HISTORY_INDEX + "="
				+ historyIndex, null) > 0;
	}

	public synchronized boolean removeAll() {
		return mDatabase.delete(DATABASE_TABLE, null, null) > 0;
	}

	private Cursor getAll() {
		return mDatabase.query(DATABASE_TABLE, new String[] { PKEY, CODE, AUTO,
				INFO, WORK_TIME, HISTORY_INDEX }, null, null, null, null, null);
	}

	public synchronized ArrayList<DiagInfo> getHistoryDetailList(
			int historyIndex) {
		ArrayList<DiagInfo> list = new ArrayList<DiagInfo>();

		Cursor cursor = mDatabase.query(DATABASE_TABLE, new String[] { PKEY,
				CODE, AUTO, INFO, WORK_TIME, HISTORY_INDEX }, HISTORY_INDEX
				+ " = " + historyIndex, null, null, null, null);
		if (cursor != null && cursor.getCount() > 0) {
			cursor.moveToFirst();
			while (!cursor.isAfterLast()) {
				DiagInfo info = new DiagInfo();
				info.setPkey(cursor.getInt(0));
				info.setCode(cursor.getString(1));
				info.setAuto(cursor.getInt(2));
				info.setInfo(cursor.getString(3));
				info.setWorkTime(cursor.getString(4));
				info.setHistoryIndex(cursor.getInt(5));
				list.add(info);
				cursor.moveToNext();
			}
			cursor.close();
		}

		return list;
	}

	public synchronized ArrayList<DiagInfo> getInfoList() {
		ArrayList<DiagInfo> list = new ArrayList<DiagInfo>();
		DiagInfo info;
		Cursor cursor = getAll();
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			info = new DiagInfo();
			info.setPkey(cursor.getInt(0));
			info.setCode(cursor.getString(1));
			info.setAuto(cursor.getInt(2));
			info.setInfo(cursor.getString(3));
			info.setWorkTime(cursor.getString(4));
			info.setHistoryIndex(cursor.getInt(5));
			list.add(info);
			cursor.moveToNext();
		}
		cursor.close();
		return list;
	}

	// check whether given DTC Code exists in the history db with same date
	public synchronized boolean checkDuplication(String code) {

		try {

			String curDate = "%" + getCurrentDate() + "%";
			Cursor cursor = mDatabase.query(DATABASE_TABLE, new String[] {
					PKEY, CODE, AUTO, INFO, WORK_TIME },
					CODE + " = ? AND " + WORK_TIME + " like ?", new String[] {
							code, curDate }, null, null, null);

			if (cursor.getCount() > 0) {
				return true;
			}
			cursor.close();

		} catch (SQLiteException e) {

		}

		return false;
	}

	private String getCurrentDate() {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		return simpleDateFormat.format(date).toString();
	}

}
